package com.xjscrm.console.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTransDto;
import com.uzai.mobile.collect.api.dto.msg.MicaFriendTalkMsg;
import com.uzai.mobile.collect.api.dto.msg.MicaPublishSnsResultMsg;
import com.uzai.mobile.collect.api.dto.trigger.talk.MicaTalkContentLinkMsg;
import com.uzai.mobile.collect.api.enums.MicaMsgType;
import com.uzai.trace.TraceGenerate;
import com.xjscrm.console.param.group.JoinGroupInviteDto;
import com.xjscrm.console.service.customer.circle.CircleTaskPublishService;
import com.xjscrm.console.service.customer.group.WwGroupInviteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@RocketMQMessageListener(topic = MicaTopics.CONSOLE_TALK_TOPIC, consumerGroup = MicaTopics.CONSOLE_TALK_TOPIC, messageModel = MessageModel.CLUSTERING)
@Component
@Slf4j
public class MessageConsumer implements RocketMQListener<MicaTransDto> {

    @Autowired
    private WwGroupInviteService wwGroupInviteService;
    @Autowired
    private CircleTaskPublishService circleTaskPublishService;

    @Override
    @TraceGenerate
    public void onMessage(MicaTransDto message) {

        log.info("MQ消息: {}", JSON.toJSONString(message));

        Integer msgType = message.getMsgType();
        MicaMsgType byValue = MicaMsgType.getByValue(msgType);

        //聊天消息中获取 群邀请链接
        if (byValue.equals(MicaMsgType.FRIEND_TALK)) {
            MicaFriendTalkMsg micaFriendTalkMsg = JSONObject.parseObject(message.getData().toString(), MicaFriendTalkMsg.class);
            if (micaFriendTalkMsg.getLinkContent() != null) {
                MicaTalkContentLinkMsg linkContent = micaFriendTalkMsg.getLinkContent();
                if (!StringUtils.isBlank(linkContent.getTitle()) && !StringUtils.isBlank(linkContent.getUrl())) {
                    if (linkContent.getTitle().contains("群聊") && linkContent.getTitle().contains("加入") && linkContent.getTitle().contains("群聊")) {
                        JoinGroupInviteDto joinGroupInviteDto = new JoinGroupInviteDto();
                        joinGroupInviteDto.setMerId(message.getMerId());
                        joinGroupInviteDto.setDeviceId(message.getDeviceId());
                        joinGroupInviteDto.setConversationId(micaFriendTalkMsg.getConversationId());
                        joinGroupInviteDto.setInviteWxId(micaFriendTalkMsg.getConversationId());
                        joinGroupInviteDto.setGroupInviteUrl(linkContent.getUrl());
                        wwGroupInviteService.joinGroupInvite(joinGroupInviteDto);
                    }
                }
            }
        } else if (byValue.equals(MicaMsgType.PUBLISH_SNS_TASK)) {
            Object data = message.getData();
            //
            MicaPublishSnsResultMsg publishSnsResultMsg = JSON.parseObject(data.toString(), MicaPublishSnsResultMsg.class);
            //
            circleTaskPublishService.updatePublishStatus(message.getMerId(), publishSnsResultMsg);
        }
    }
}